define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部状态',
                name: 'status',
                filters: dicts.status
            });

            utils.addFilterDropdown(pane, {
                text: '全部域名',
                name: 'domain',
                dropdown: dicts.account_domain
            });
        }

        /**
         * 统计
         */
        {
            var analyse = function () {
                adminApi('accounts_analyse').then(function (rows) {
                    $('.content-header .filter-status[data-name="status"] .badge').text('');
                    var total = 0;
                    for (var i in rows) {
                        var row = rows[i];
                        $('.status-' + row.status, pane).text(row.count || 0);
                        total += (row.count || 0);
                    }
                    $('.status-all', pane).text(total);
                });
            }
            analyse();
        }

        //全局变量
        var fields_arrays = [
            ['domain', 'select', '域名', false, dicts.account_domain],
            ['nickname', 'text', '账号别名'],
            ['username', 'text', '账号名'],
            ['password', 'text', '密码'],
            ['cookies', 'textarea', 'Cookie'],
            ['ext_data', 'textarea', '数据'],
            ['status', 'select', '状态', false, dicts.status],
            ['user_id', 'text', '账号编号'],
            ['note', 'textarea', '备注']
        ];
        var addFields = utils.convertFields(fields_arrays),
            editFields = utils.convertFields(fields_arrays);

        //初始化工具
        utils.addTools(pane, [
            {className: 'add-account-btn', icon: 'fa-plus', text: '添加账号'},
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}
        ]);
        $(pane).on('click', '.add-account-btn', function () {
            utils.formAdd(table, {
                name: '账号', fields: addFields, columns: 6,
                fnServerData: function (paramData) {
                    return adminApi('accounts_add', paramData);
                }
            });
        }).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["user_id", "用户", false, function (i, t, row) {
                    var user = row.user;
                    if (!user) {
                        return '';
                    }
                    return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div>';
                }, 'filterable'],
                ["domain", "域名", false, utils.dictRender('account_domain')],
                ["nickname", "别名", false],
                ["username", "账号", false],
                ["note", "备注", false, utils.nameRender()],
                ["status", "状态", false, utils.dictRender('status')],
                ["updated_at", "更新时间", false, utils.dateTimeRender()],
                ["created_at", "创建时间", false, utils.dateTimeRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('accounts_browse', paramData);
            }
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 0, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '编辑账号', icon: 'fa-edit', className: 'edit-ope'}
                    ];
                    if (data.status === 'Deactivated') {
                        opts.push({text: '启用账号', className: 'active-opt btn-success'});
                    } else {
                        opts.push({text: '禁用账号', className: 'deactivate-opt btn-warning'});
                    }
                    opts.push({text: '删除账号', icon: 'fa-trash-o', className: 'delete-ope btn-danger'});
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', toggleHandler).on('click', '.active-opt', utils.tableEvent(table, function (idx, rowData) {
            //启用账号
            return adminApi('accounts_edit', {
                id: rowData.id,
                status: 'Active'
            }).then(utils.handEdit(table, {idx: idx, name: '账号'}, true, toggleHandler));
        })).on('click', '.deactivate-opt', utils.tableEvent(table, function (idx, rowData) {
            //禁用账号
            return adminApi('accounts_edit', {
                id: rowData.id,
                status: 'Deactivated'
            }).then(utils.handEdit(table, {idx: idx, name: '账号'}, true, toggleHandler));
        })).on('click', '.edit-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑账号
            adminApi('accounts_load', {id: rowData.id}).then(function (data) {
                utils.formEdit(table, {
                    idx: idx, name: (rowData.nickname || rowData.username), fields: editFields, columns: 6, data: data,
                    fnServerData: function (paramData) {
                        return adminApi('accounts_edit', paramData);
                    }
                });
            });
        })).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除账号
            utils.confirmation(this, {
                placement: 'left',
                optName: '删除账号',
                title: '你确定删除账号?',
                fnConfirm: function () {
                    return adminApi('accounts_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '账号'})).then(function (result) {
                        analyse();
                        return result;
                    });
                }
            });
        }));
    }
});